package link.wangkui.threadlocal;

public class ThreadLocalDemo2 {
    static class Index {
        int count;
        public void increase() {
            count++;
        }

        public Index(int count) {
            this.count = count;
        }
    }
    private static final ThreadLocal<Index> local = new ThreadLocal<Index>() {
        @Override
        protected Index initialValue() {
            return new Index(0);
        }
    };

    public static void main(String[] args) {
        Thread[] threads = new Thread[5];
        for (int i = 0; i < 5; i++) {
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    local.get().increase();
                    System.out.println(Thread.currentThread().getName() + ":" + local.get().count);
                }
            }, "Threads_" + i);
        }
        for (int i = 0; i < 5; i++) {
            threads[i].start();
        }
    }
}
